#include <bits/stdc++.h>
// 2025/07/20
// tag: 
// Author: Forever
using namespace std;
using int64 = int64_t;
#define int long long

struct process{
    int id, priority, st, t;
    process(){
        id = priority = st = t = 0;
    }
    bool operator<(const process& rhs) const {
        if (priority != rhs.priority)
            return priority < rhs.priority;
        return id > rhs.id;
    }
};

int current_time = 0;
process now, last;
priority_queue<process> wait_list;

void solve(int t){
    while (t && !wait_list.empty()){
        process res = wait_list.top();
        wait_list.pop();
        if (res.t <= t){
            t -= res.t, current_time += res.t;
            cout << res.id << " " << current_time << endl;
        }
        else{
            res.t -= t, t = 0;
            current_time += t;
            wait_list.push(res);
        }
    }
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> last.id >> last.st >> last.t >> last.priority;
    current_time = last.st, now = last;
    while (cin >> last.id >> last.st >> last.t >> last.priority) {
        int gap = last.st - current_time;
        if (now.id != 0){
            if (now.t <= gap){
                current_time += now.t;
                cout << now.id << " " << current_time << endl;
                now = process();
                solve(last.st - current_time);
                current_time = last.st;
            }
            else{
                now.t -= gap;
                current_time = last.st;
            }
        }
        else {
            solve(gap);
            current_time = last.st;
        }
        if (now.id == 0 && !wait_list.empty()) {
            now = wait_list.top();
            wait_list.pop();
        }
        if (now.id == 0){
            now = last;
        }
        else {
            if (now.priority < last.priority){
                wait_list.push(now);
                now = last;
            }
            else{
                wait_list.push(last);
            }
        }
    }
    while (now.id != 0 || !wait_list.empty()){
        if (now.id != 0){
            current_time += now.t;
            cout << now.id << " " << current_time << endl;
            now = process();
        }
        if (!wait_list.empty()){
            now = wait_list.top();
            wait_list.pop();
        }
    }
    return 0;
}